﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;
using System.Web.Services;

public partial class back_ViewProperties : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    static IDataRepository dataObject = new DataRepository();
    Property property;
    User user;
    Builder builder;
    Architect architect;
    List<Architect> architects = new List<Architect>();
    List<Builder> builders = new List<Builder>();
    List<Property> properties = new List<Property>();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindProperties();
            bindBuilders();
            bindArchitects();
        }
    }

    private void bindProperties()
    {
        property = new Property(_dataObject);
        properties = property.GetAllProperties();

        if (properties.Count > 0)
        {
            grdProperties.Visible = true;
            grdProperties.DataSource = properties;
            grdProperties.DataBind();
        }
        else
        {
            grdProperties.Visible = false;
            divRed.InnerHtml = "<strong>Oh snap!</strong>&nbsp;No Records Found!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    private void bindBuilders()
    {
        builder = new Builder(_dataObject);
        builders = builder.GetAllBuilders();

        if (builders.Count > 0)
        {
            grdBuilders.Visible = true;
            grdBuilders.DataSource = builders;
            grdBuilders.DataBind();
        }
        else
        {
            grdBuilders.Visible = false;
            //divRed.InnerHtml = "<strong>Oh snap!</strong>&nbsp;No Records Found!";
            //divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    private void bindArchitects()
    {
        architect = new Architect(_dataObject);
        architects = architect.GetAllArchitects();

        if (architects.Count > 0)
        {
            grdArchitects.Visible = true;
            grdArchitects.DataSource = architects;
            grdArchitects.DataBind();
        }
        else
        {
            grdArchitects.Visible = false;
            //divRed.InnerHtml = "<strong>Oh snap!</strong>&nbsp;No Records Found!";
            //divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    [WebMethod()]
    public static string assignBuilders(string propertyId, string builderId)
    {
        Property prop = new Property(dataObject);
        prop.Id = Convert.ToInt32(propertyId);
        prop.BuilderId = Convert.ToInt32(builderId);
        prop.AssignBuilder();

        return "OK";
    }
    [WebMethod()]
    public static string assignArchitects(string propertyId, string architectId)
    {
        Property prop = new Property(dataObject);
        prop.Id = Convert.ToInt32(propertyId);
        prop.ArchitectId = Convert.ToInt32(architectId);
        prop.AssignArchitect();

        return "OK";
    }
    protected void grdProperties_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        grdProperties.PageIndex = e.NewPageIndex;
        bindProperties();
    }
    protected void grdProperties_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
        {
            System.Web.UI.WebControls.Label hdnPropertyId = (System.Web.UI.WebControls.Label)e.Row.FindControl("hdnPropertyId");
            System.Web.UI.WebControls.Label hdnBuilderId = (System.Web.UI.WebControls.Label)e.Row.FindControl("hdnBuilderId");
            System.Web.UI.WebControls.Label hdnArchitectId = (System.Web.UI.WebControls.Label)e.Row.FindControl("hdnArchitectId");
            System.Web.UI.WebControls.Label lblUserId = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblUserId");
            System.Web.UI.WebControls.Label lblUser = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblUser");
            System.Web.UI.WebControls.Label lblBuilder = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblBuilder");
            System.Web.UI.WebControls.Label lblArchitect = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblArchitect");

            System.Web.UI.WebControls.Button btnBuilders = (System.Web.UI.WebControls.Button)e.Row.FindControl("btnBuilders");
            System.Web.UI.WebControls.Button btnArchitects = (System.Web.UI.WebControls.Button)e.Row.FindControl("btnArchitects");

            btnBuilders.Attributes.Add("OnClick", "lanchBuilderPopUp(" + hdnPropertyId.Text + "); return false;");
            btnArchitects.Attributes.Add("OnClick", "lanchArchitectPopUp(" + hdnPropertyId.Text + "); return false;");

            user = new CDB.LinqModel.User(_dataObject);
            user.Id = Convert.ToInt32(lblUserId.Text);
            user.GetUserById();

            if (user.Id != 0)
            {
                lblUser.Text = user.FirstName + " " + user.LastName;

                if (user.ServiceRequired != null)
                {
                    switch (user.ServiceRequired)
                    {
                        case "Architectural Only":
                            btnArchitects.Visible = true;
                            break;
                        case "Construction Only":
                            btnBuilders.Visible = true;
                            break;
                        case "Sanctions & Permit":
                            break;
                        case "Complete Package":
                            btnArchitects.Visible = true;
                            btnBuilders.Visible = true;
                            break;
                    }
                }
            }

            Architect architect = new Architect(_dataObject);
            architect.Id = hdnArchitectId.Text == "" ? 0 : Convert.ToInt32(hdnArchitectId.Text);
            architect.GetArchitectById();

            if (architect.Id != 0)
            {
                lblArchitect.Text = "Architect: " + architect.Firm;
            }

            Builder builder = new Builder(_dataObject);
            builder.Id = hdnBuilderId.Text == "" ? 0 : Convert.ToInt32(hdnBuilderId.Text);
            builder.GetBuilderById();

            if (builder.Id != 0)
            {
                lblBuilder.Text = "Builder: " + builder.Name;
            }
        }
    }
}